package Test;

import java.util.concurrent.*;

public class TestDemo6 {
    public static void main(String[] args) throws InterruptedException {
        CompletableFuture.supplyAsync(()->{
            System.out.println(Thread.currentThread().getName()+" come in");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return ThreadLocalRandom.current().nextInt(10);
        }).whenComplete((v,e)->{
            if(e == null) {
                System.out.println("更新"+v);
            }
        }).exceptionally(e -> {
            e.printStackTrace();
            System.out.println("异常信息"+e);
            return null;
        });

        TimeUnit.SECONDS.sleep(3);


    }
    public static void main2(String[] args) throws ExecutionException, InterruptedException {

        CompletableFuture<Integer> completableFuture =  CompletableFuture.supplyAsync(()->{
            System.out.println(Thread.currentThread().getName()+" come in");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return ThreadLocalRandom.current().nextInt(10);
        });
        System.out.println(Thread.currentThread().getName()+"先去忙");
        System.out.println(completableFuture.get());


    }
    public static void main1(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> futureTask = new FutureTask<>(new MyCallable());
        Thread thread = new Thread(futureTask);
        thread.start();
        System.out.println(futureTask.isDone());
        String s = futureTask.get();
        System.out.println(s);
    }
}

class MyCallable implements Callable<String> {
    @Override
    public String call() throws Exception {
        System.out.println("call in");
        return "call";
    }
}
